package com.soluzionasf.zeus;

import org.eclipse.core.commands.Command;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ContributionItemFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.commands.ICommandService;

import com.soluzionasf.zeus.datamanager.actions.OpenDataInputAction;
import com.soluzionasf.zeus.uitoolkit.actions.NonModalNewWizardDropDownAction;
import com.soluzionasf.zeus.uitoolkit.actions.NonModalNewWizardMenu;
import com.soluzionasf.zeus.uitoolkit.handlers.NonModalWizardHandler;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
	private IWorkbenchAction quitAction;
	private IWorkbenchAction helpContentsAction;
	private IWorkbenchAction introAction;
	private IWorkbenchAction newWindowAction;
	private IContributionItem perspectiveList;
	private IContributionItem viewList;
	private IWorkbenchAction savePerspectiveAction;
	private Action openMonitorExplotacionAction;
	private IContributionItem openWindowsAction;
	private IWorkbenchAction preferencesAction;
	private IWorkbenchAction resetPerspectiveAction;
	private IWorkbenchAction aboutAction;
	private IWorkbenchAction newWizardDropDownAction;

	private NonModalNewWizardMenu newWizardMenu;
	private IAction lockToolBarAction;
	private IAction editActionSetAction;
	private Action dataInputAction;

	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
		configurer.getWindowConfigurer().getWindow();
	}

	private MenuManager createFileMenu() {
		MenuManager menu = new MenuManager(
				Messages.ApplicationActionBarAdvisor_file,
				IWorkbenchActionConstants.M_FILE);
		menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
		{
			String newText = Messages.ApplicationActionBarAdvisor__new;
			String newId = ActionFactory.NEW.getId();
			MenuManager newMenu = new MenuManager(newText, newId);
			newMenu.add(new Separator(newId));
			newMenu.add(newWizardMenu);
			newMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
			menu.add(newMenu);
		}

		menu.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.SAVE_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.PRINT_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.OPEN_EXT));
		menu.add(new Separator());
		menu.add(new GroupMarker(IWorkbenchActionConstants.IMPORT_EXT));
		menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		menu.add(new Separator());
		menu.add(quitAction);
		menu.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
		return menu;
	}

	private MenuManager createHelpMenu() {
		MenuManager menu = new MenuManager(
				Messages.ApplicationActionBarAdvisor_help,
				IWorkbenchActionConstants.M_HELP);
		// menu.add(new GroupMarker("group.intro")); //$NON-NLS-1$
		// menu.add(introAction);
		menu.add(new GroupMarker("group.intro.ext")); //$NON-NLS-1$
		menu.add(new Separator("group.main")); //$NON-NLS-1$
		menu.add(helpContentsAction);
		menu.add(new Separator("group.assist")); //$NON-NLS-1$
		menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
		menu.add(new GroupMarker("group.main.ext")); //$NON-NLS-1$
		menu.add(new GroupMarker("group.tutorials")); //$NON-NLS-1$
		menu.add(new GroupMarker("group.tools")); //$NON-NLS-1$
		menu.add(new GroupMarker("group.updates")); //$NON-NLS-1$
		menu.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
		menu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

		menu.add(new Separator("group.about")); //$NON-NLS-1$
		menu.add(aboutAction);
		menu.add(new GroupMarker("group.about.ext")); //$NON-NLS-1$
		return menu;
	}

	private MenuManager createWindowMenu() {
		MenuManager menu = new MenuManager(
				Messages.ApplicationActionBarAdvisor_window,
				IWorkbenchActionConstants.M_WINDOW);

		menu.add(newWindowAction);

		menu.add(new Separator());
		MenuManager openPerspectiveMenu = new MenuManager(
				Messages.ApplicationActionBarAdvisor__openPerspective,
				"openPerspective"); //$NON-NLS-1$
		openPerspectiveMenu.add(perspectiveList);
		menu.add(openPerspectiveMenu);

		MenuManager showViewMenu = new MenuManager(
				Messages.ApplicationActionBarAdvisor__showView, "showView"); //$NON-NLS-1$
		showViewMenu.add(viewList);
		menu.add(showViewMenu);

		menu.add(new Separator());
		menu.add(savePerspectiveAction);
		menu.add(resetPerspectiveAction);

		menu.add(new Separator());
		menu.add(openMonitorExplotacionAction);

		menu.add(openWindowsAction);
		menu.add(new Separator());

		menu.add(preferencesAction);

		return menu;
	}

	private MenuManager createOtherMenu() {
		MenuManager menu = new MenuManager(
				Messages.ApplicationActionBarAdvisor_Contributions_Menu,
				"other"); //$NON-NLS-1$

		menu.add(dataInputAction);

		return menu;
	}

	protected void fillMenuBar(IMenuManager menuBar) {
		menuBar.add(createFileMenu());
		menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		menuBar.add(createWindowMenu());
		menuBar.add(createOtherMenu());
		menuBar.add(createHelpMenu());
	}

	protected void makeActions(final IWorkbenchWindow window) {
		newWizardMenu = new NonModalNewWizardMenu(window, null);

		quitAction = ActionFactory.QUIT.create(window);
		register(quitAction);

		perspectiveList = ContributionItemFactory.PERSPECTIVES_SHORTLIST
				.create(window);

		viewList = ContributionItemFactory.VIEWS_SHORTLIST.create(window);

		newWindowAction = ActionFactory.OPEN_NEW_WINDOW.create(window);
		register(newWindowAction);

		savePerspectiveAction = ActionFactory.SAVE_PERSPECTIVE.create(window);
		register(savePerspectiveAction);

		resetPerspectiveAction = ActionFactory.RESET_PERSPECTIVE.create(window);
		register(resetPerspectiveAction);

		
		openMonitorExplotacionAction = new Action(
				Messages.ApplicationActionBarAdvisor_Open_monitor) {
			public void run() {
				try {
					boolean abrir = true;
					IWorkbenchWindow[] ventanas = PlatformUI.getWorkbench()
							.getWorkbenchWindows();
					for (int i = 0; i < ventanas.length; i++) {
						if (ventanas[i]
								.getActivePage()
								.getPerspective()
								.getId()
								.equals(
										"com.soluzionasf.zeus.realTimeMonitor.RealTimeMonitorPerspective")) { //$NON-NLS-1$
							abrir = false;
						}
					}

					if (abrir) {
						IWorkbenchWindow monitor = PlatformUI
								.getWorkbench()
								.openWorkbenchWindow(
										"com.soluzionasf.zeus.realTimeMonitor.RealTimeMonitorPerspective", null); //$NON-NLS-1$

					} else {
						MessageDialog
								.openInformation(
										PlatformUI.getWorkbench().getDisplay()
												.getActiveShell(),
										Messages.ApplicationActionBarAdvisor_Attention,
										Messages.ApplicationActionBarAdvisor_Only_one_monitor_allowed
												+ "."); //$NON-NLS-1$
					}

				} catch (WorkbenchException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		
		openWindowsAction = ContributionItemFactory.OPEN_WINDOWS.create(window);

		preferencesAction = ActionFactory.PREFERENCES.create(window);
		register(preferencesAction);

		// introAction = ActionFactory.INTRO.create(window);
		// register(introAction);

		helpContentsAction = ActionFactory.HELP_CONTENTS.create(window);
		register(helpContentsAction);

		aboutAction = ActionFactory.ABOUT.create(window);
		register(aboutAction);

		lockToolBarAction = ActionFactory.LOCK_TOOL_BAR.create(window);
		register(lockToolBarAction);

		editActionSetAction = ActionFactory.EDIT_ACTION_SETS.create(window);
		register(editActionSetAction);

		newWizardDropDownAction = new NonModalNewWizardDropDownAction(window);
		newWizardDropDownAction.setId("newWizardDropDown"); //$NON-NLS-1$
		register(newWizardDropDownAction);

		dataInputAction = new OpenDataInputAction(window);
		register(dataInputAction);

		ICommandService commandService = (ICommandService) PlatformUI
				.getWorkbench().getService(ICommandService.class);
		Command newWizardCommand = commandService
				.getCommand("org.eclipse.ui.newWizard"); //$NON-NLS-1$
		newWizardCommand.setHandler(new NonModalWizardHandler.New());
	}

	protected void fillCoolBar(ICoolBarManager coolBar) {
		coolBar.add(new GroupMarker("group.file")); //$NON-NLS-1$
		{ // File Group
			IToolBarManager fileToolBar = new ToolBarManager(SWT.FLAT
					| SWT.RIGHT);
			fileToolBar.add(new Separator(IWorkbenchActionConstants.NEW_GROUP));
			fileToolBar.add(newWizardDropDownAction);
			fileToolBar.add(new GroupMarker(IWorkbenchActionConstants.NEW_EXT));
			fileToolBar.add(new GroupMarker(
					IWorkbenchActionConstants.SAVE_GROUP));
			fileToolBar
					.add(new GroupMarker(IWorkbenchActionConstants.SAVE_EXT));
			fileToolBar
					.add(new GroupMarker(IWorkbenchActionConstants.PRINT_EXT));
			fileToolBar.add(new Separator(
					IWorkbenchActionConstants.MB_ADDITIONS));

			coolBar.add(new ToolBarContributionItem(fileToolBar,
					IWorkbenchActionConstants.TOOLBAR_FILE));
		}

		coolBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));

		coolBar.add(new GroupMarker(IWorkbenchActionConstants.GROUP_HELP));

		{ // Help group
			IToolBarManager helpToolBar = new ToolBarManager(SWT.FLAT
					| SWT.RIGHT);
			helpToolBar
					.add(new Separator(IWorkbenchActionConstants.GROUP_HELP));
			helpToolBar
					.add(new GroupMarker(IWorkbenchActionConstants.GROUP_APP));

			coolBar.add(new ToolBarContributionItem(helpToolBar,
					IWorkbenchActionConstants.TOOLBAR_HELP));
		}
	}

}
